package me.guizhi.work.dao;

import me.guizhi.work.domain.User;
import me.guizhi.work.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao{
    @Override
    public User login(String username, String password) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select * from user where username=? and password=?";
        Object[] param = {username,password};
        User user = qr.query(sql,new BeanHandler<>(User.class),param);
        return user;
    }

    @Override
    public void addUser(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into user (username,password,sex,birthday) values (?, ?, ?, ?)";
        Object[] param = {user.getUsername(), user.getPassword(), user.getSex(), user.getBirthday()};
        qr.update(sql,param);
    }
}
